from pathlib import Path
from PIL import Image

# 1. 定位到项目根目录（learntorch/）
ROOT      = Path(__file__).resolve().parent.parent   # D:\codes\learntorch
src_root  = ROOT / 'datas'
dst_root  = ROOT / 'datas128'
size      = 128
resample  = Image.Resampling.BILINEAR

# 2. 开始 resize
for split in ['seg_train', 'seg_val']:
    for cls_dir in (src_root / split).iterdir():
        if not cls_dir.is_dir():
            continue
        new_dir = dst_root / split / cls_dir.name
        new_dir.mkdir(parents=True, exist_ok=True)
        for img_path in cls_dir.glob('*'):
            try:
                img = Image.open(img_path).convert('RGB')
                img = img.resize((size, size), resample)
                img.save(new_dir / img_path.name)
            except Exception as e:
                print('跳过破损图片:', img_path, e)

print('✅ 全部 resize 完成，结果在', dst_root)